package com.example.springthymeleafproject.controller;

import com.example.springthymeleafproject.model.Device;
import com.example.springthymeleafproject.service.DepartmentService;
import com.example.springthymeleafproject.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class DeviceController {
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private DepartmentService departmentService;

    //主页面
//    @GetMapping("/")
//    public String viewHomePage(Model model) {
//        model.addAttribute("listDevice", deviceService.getAllDevice());
//        return "index";
//    }
    //设备分页主页面
    @GetMapping("/")
    //默认以id作为排序列，且为降序
    public String viewHomePage(Model model) {
        return searchDevicePaginated(1, "id", "asc", null, null, model);
    }


    // 显示添加设备页面
    @GetMapping("/addDevice")
    public String addDeviceForm(Model model) {
        //创建模型属性来绑定表单数据
        Device device = new Device();
        model.addAttribute("device", device);
        model.addAttribute("departments", departmentService.getAllDepartment());
        return "addDevice";
    }

    //数据保存
    //注意一定要用PostMapping，不然会出现无法保存数据
    @PostMapping("/saveDevice")
    public String saveDevice(@ModelAttribute("device") Device device) {
        //提交保存数据
        deviceService.saveDevice(device);
        return "redirect:/";
    }


    //更新设备
    @GetMapping("/updateDevice/{id}")
    public String UpdateDevice(@PathVariable(value = "id") long id, Model model) {
        //从服务中获取设备
        Device device = deviceService.getDeviceId(id);

        //将device设置为模型属性以预填充表单
        model.addAttribute("device", device);
        model.addAttribute("departments", departmentService.getAllDepartment());

        return "updateDevice";
    }

    // 删除设备
    @GetMapping("/deleteDevice/{id}")
    public String deleteDevice(@PathVariable(value = "id") long id) {
        this.deviceService.deleteDeviceById(id);
        return "redirect:/";
    }


    //获取分页数据并对通过字段排序排序、模糊查询
    @GetMapping("/page/{pageNo}")
    public String searchDevicePaginated(
            // value 指定了绑定的路径参数名称，int 表示将路径参数转成整型类型并绑定到 pageNo 参数上。
            @PathVariable(value = "pageNo") int pageNo,
            //@RequestParam 注解表示这个参数是一个查询参数，用来指定排序字段和排序方向。其中：
            //"sortField" 表示查询参数的名称，也就是排序的字段。
            //"sortDir" 表示查询参数的名称，也就是排序的方向。（升序|降序）
            @RequestParam("sortField") String sortField,
            @RequestParam("sortDir") String sortDir,
            @RequestParam(required = false) String deviceName,
            @RequestParam(required = false) Long departmentID,
            Model model
    ) {
        //页面中放几个数据
        int pageSize = 7;
        //employeeService.findPaginated(pageNo, pageSize, sortField, sortDir)
        // 方法通过传递的参数 pageNo、pageSize、sortField 和 sortDir 来实现分页查询，并返回一个 Page<Employee> 对象，
        // 其中包含指定 '页码和数量' 的员工数据。
        Page<Device> devicePage;
        if (deviceName != null && !deviceName.isEmpty()) {
            devicePage = deviceService.searchDevicesPaginated(pageNo, pageSize, sortField, sortDir, deviceName);
        } else if (departmentID != null) {
            devicePage = deviceService.searchDevicesByDepartmentIdPaginated(pageNo, pageSize, sortField, sortDir, departmentID);
        } else {
            devicePage = deviceService.searchDevicesPaginated(pageNo, pageSize, sortField, sortDir, null);
        }
        //page.getContent() 方法可以获取到当前页的员工列表，然后将分页和排序相关的数据和员工列表数据传递到前端视图层页面。
        List<Device> listDevice = devicePage.getContent();

        model.addAttribute("currentPage", pageNo);
        model.addAttribute("totalPages", devicePage.getTotalPages());
        model.addAttribute("totalItems", devicePage.getTotalElements());

        model.addAttribute("sortField", sortField);
        model.addAttribute("sortDir", sortDir);
        model.addAttribute("reverseSortDir", sortDir.equals("asc") ? "desc" : "asc");
        model.addAttribute("deviceName", deviceName);
        model.addAttribute("departmentID", departmentID);

        model.addAttribute("listDevice", listDevice);
        return "index";
    }

}
